package tank1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.util.Scanner;
import java.util.Vector;

/**
 * Version: 1.1
 * Author: TangJun
 * Date: 2024-07-30
 */
public class TankPanel extends JPanel implements KeyListener, Runnable {
    private Tank tank;
    private final Vector<Tank> enemyTank = new Vector<Tank>();
    private Vector<Node> nodes;
    private final Vector<Bomb> bombs = new Vector<Bomb>();
    Image image1;
    Image image2;
    Image image3;
    //开始新游戏
    public void NewGame() {
        Recorder.setTanks(enemyTank);
        tank = new Tank(1000, 500, 0);
        tank.setEnemies(enemyTank);
        //初始敌方坦克数量
        int enemyTankNum = 5;
        for (int i = 0; i < enemyTankNum; i++) {
            Tank tank = new Tank(500 + 100 * i, 300, 1);
            tank.setDirect(2);
            tank.setEnemies((enemyTank));
            enemyTank.add(tank);
            new Thread(tank).start();
        }
        //new Thread(tank).start();//AI托管
        image1 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_1.gif"));
        image2 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_2.gif"));
        image3 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_3.gif"));
    }

    //继续游戏
    public void ContinueGame() {
        Recorder.setTanks(enemyTank);
        //初始化我方坦克
        tank = new Tank(1000, 500, 0);
        tank.setEnemies(enemyTank);
        tank.setDirect(2);
        nodes = Recorder.getNodes();
        //初始敌方坦克数量
        for (Node node : nodes) {
            Tank tank = new Tank(node.getX(), node.getY(), node.getDirect());
            tank.setEnemies((enemyTank));
            tank.setType(1);
            enemyTank.add(tank);
            new Thread(tank).start();
        }
        //new Thread(tank).start();//AI托管
        image1 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_1.gif"));
        image2 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_2.gif"));
        image3 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_3.gif"));
    }

    public TankPanel() {
        nodes = Recorder.getNodes();
        System.out.println("1.开始新游戏     2.继续游戏");
        System.out.print("请输入游戏模式:>");
        Scanner scan = new Scanner(System.in);
        switch (scan.nextInt()) {
            case 1:
                NewGame();
                break;
            case 2:
                if (new File(Recorder.getFileRecord()).exists()) {
                    Recorder.Load();
                    ContinueGame();
                } else {
                    NewGame();
                    System.out.println("没有上局游戏数据，只能开启新游戏");
                }
                break;
            default:
                System.out.println("没这个选项");
                System.exit(0);
                break;
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //配置界面
        g.fillRect(0, 0, TankFrame.WIDTH - 500, TankFrame.HEIGHT);
        drawInfo(g);
        tank.setType(0);
        //画我方坦克
//        if (tank.isLive()) {
            drawTank(g, tank);
        /*} else {
            System.out.println("游戏结束");
            //System.exit(0);
        }*/

        //画敌方坦克
        enemyTank.removeIf(enemy -> !enemy.isLive());
        for (Tank enemy : enemyTank) {
            drawTank(g, enemy);
            drawBulletAll(g, enemy.getBullets());
        }
        //画子弹
        drawBulletAll(g, tank.getBullets());
        //画爆炸效果
        drawBomb(g, bombs);
    }

    //画游戏信息
    public void drawInfo(Graphics g) {
        g.setColor(Color.black);
        g.setFont(new Font("楷体", Font.BOLD, 40));
        g.drawString("已击败坦克数量：", TankFrame.WIDTH - 480, 50);
        g.drawString("" + Recorder.getDestroyNum(), TankFrame.WIDTH - 350, 200);
        drawTank(g, new Tank(TankFrame.WIDTH - 480, 150, 1));
        g.setColor(Color.black);
    }

    //画爆炸效果
    public void drawBomb(Graphics g, Vector<Bomb> bombs) {
        for (int i = 0; i < bombs.size(); i++) {
            Bomb bomb = bombs.get(i);
            int life = bomb.getLife();
            //画一个爆炸对象
            if (life >= Bomb.LIFE/3*2) {
                g.drawImage(image1, bomb.getX(), bomb.getY(), bomb.getSize(), bomb.getSize(), this);
            } else if (life >= Bomb.LIFE/3) {
                g.drawImage(image2, bomb.getX(), bomb.getY(), bomb.getSize(), bomb.getSize(), this);
            } else {
                g.drawImage(image3, bomb.getX(), bomb.getY(), bomb.getSize(), bomb.getSize(), this);
            }
            bomb.lifeDown();
            if (life == 0) {
                bombs.remove(i);
            }
        }
    }
    //画多颗子弹
    public void drawBulletAll(Graphics g, Vector<Bullet> bullets) {
        bullets.removeIf(bullet -> !bullet.isLive());
        for (Bullet bullet : bullets) {
            drawBullet(g, bullet);
        }
    }

    //画一颗子弹
    public void drawBullet(Graphics g, Bullet bullet) {
        if (bullet != null) {
            g.setColor(bullet.getColor());
            g.fillOval(bullet.getX(), bullet.getY(), bullet.getSize(),bullet.getSize());
        }
    }
    public void drawTank(Graphics g, Tank tank) {
        //设置画笔颜色
        switch (tank.getType()) {
            case 0:
                g.setColor(Color.green);
                break;
            case 1:
                g.setColor(Color.red);
                break;
            default:
                break;
        }

        int x = tank.getX();
        int y = tank.getY();
        //根据方向画炮管和轮子
        switch (tank.getDirect()) {
            //炮管向上
            case 0:
                g.drawLine(x + Tank.WIDTH + Tank.SIZE/2, y + Tank.HEIGHT/2,
                        x+ Tank.WIDTH + Tank.SIZE/2, y + Tank.HEIGHT/2 - Tank.LENGTH);//炮管
                g.fill3DRect(x, y, Tank.WIDTH, Tank.HEIGHT,false);//左轮
                g.fill3DRect(x + Tank.WIDTH+Tank.SIZE, y, Tank.WIDTH, Tank.HEIGHT,false);//右轮
                break;
            //炮管向下
            case 1:
                g.drawLine(x + Tank.WIDTH + Tank.SIZE/2, y + Tank.HEIGHT/2,
                        x+ Tank.WIDTH + Tank.SIZE/2, y + Tank.HEIGHT/2 + Tank.LENGTH);//炮管
                g.fill3DRect(x, y, Tank.WIDTH, Tank.HEIGHT,false);//左轮
                g.fill3DRect(x + Tank.WIDTH+Tank.SIZE, y, Tank.WIDTH, Tank.HEIGHT,false);//右轮
                break;
            //炮管向左
            case 2:
                g.drawLine(x + Tank.HEIGHT/2, y + Tank.WIDTH+Tank.SIZE/2,
                        x+ Tank.WIDTH + Tank.SIZE/2 - Tank.LENGTH, y + Tank.WIDTH+Tank.SIZE/2);
                g.fill3DRect(x, y, Tank.HEIGHT, Tank.WIDTH ,false);//左轮
                g.fill3DRect(x, y + Tank.WIDTH + Tank.SIZE, Tank.HEIGHT, Tank.WIDTH,false);//右轮
                break;
            //炮管向右
            case 3:
                g.drawLine(x + Tank.HEIGHT/2, y + Tank.WIDTH+Tank.SIZE/2,
                        x+ Tank.WIDTH + Tank.SIZE/2 + Tank.LENGTH, y + Tank.WIDTH+Tank.SIZE/2);
                g.fill3DRect(x, y, Tank.HEIGHT, Tank.WIDTH ,false);//左轮
                g.fill3DRect(x, y + Tank.WIDTH + Tank.SIZE, Tank.HEIGHT, Tank.WIDTH,false);//右轮
                break;
            default:
                break;
        }
        g.fill3DRect(x+Tank.WIDTH, y+Tank.WIDTH, Tank.SIZE, Tank.SIZE, false);//方炮台
        g.fillOval(x+Tank.WIDTH, y+Tank.WIDTH, Tank.SIZE, Tank.SIZE);//圆炮台
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
                //坦克向上
            case KeyEvent.VK_UP:
            case KeyEvent.VK_W:
                tank.MoveUp();
                break;
                //坦克向下
            case KeyEvent.VK_DOWN:
            case KeyEvent.VK_S:
                tank.MoveDown();
                break;
                //坦克向左
            case KeyEvent.VK_LEFT:
            case KeyEvent.VK_A:
                tank.MoveLeft();
                break;
                //坦克向右
            case KeyEvent.VK_RIGHT:
            case KeyEvent.VK_D:
                tank.MoveRight();
                break;
            case KeyEvent.VK_J:
                //弹匣的子弹容量为10
                if (tank.getBullets().size() < 10) {
                    tank.shot();
                }
                break;
            default:
                break;
        }
        //System.out.println("坦克（" + tank.getX() + "," + tank.getY() + ")");
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            //判断敌方坦克爆炸
            if (tank.getBullets() != null) {
                for (Tank enemy : enemyTank) {
                    boolean retEnemy = enemy.hitAll(tank.getBullets());
                    boolean retMe = tank.hitAll(enemy.getBullets());
                    //添加敌方爆炸对象
                    if (retEnemy) {
                        bombs.add(new Bomb(enemy.getX(), enemy.getY()));
                    }
                    //添加我方爆炸对象
                    if (retMe) {
                        bombs.add(new Bomb(tank.getX(), tank.getY()));
                    }

                }
            }
            //重绘
            this.repaint();
        }
    }
}
